<?php

declare(strict_types=1);

namespace App\Process\Logger;


use Hyperf\Context\Context;

class MemoryUsageProcessor extends \Monolog\Processor\MemoryUsageProcessor
{
    /**
     * {@inheritDoc}
     */
    public function __invoke(array $record): array
    {
        $memory = memory_get_usage($this->realUsage);
        if (!Context::has('memory_get_usage')) {
            Context::set('memory_get_usage', $memory);
            $usage = $memory;
        } else {
            $usage = Context::get('memory_get_usage');
        }
        Context::set('memory_get_usage', $memory);

        $usage = $memory - $usage;
        if ($this->useFormatting) {
            $usage = $this->formatBytes($usage);
        }

        $record['extra']['memory_usage'] = $usage;

        return $record;
    }
}
